<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root"></div>

    <script src="https://unpkg.zhimg.com/react@17.0.2/umd/react.development.js"></script>
    <script src="https://unpkg.zhimg.com/react-dom@17.0.2/umd/react-dom.development.js"></script>
    <script src="https://unpkg.zhimg.com/@babel/standalone/babel.min.js"></script>
    <script type="text/babel">
        class Life extends React.Component {

            state = {
                opacity: 1
            }

            // 组件挂载完成
            componentDidMount() {
                this.timer = setInterval(() => {
                    let {opacity} = this.state

                    if (opacity <= 0) {
                        opacity = 1
                    }

                    opacity -= .1

                    this.setState({
                        opacity: opacity
                    })
                }, 200);
            }

            // 组件将要卸载
            componentWillUnmount() {
                clearInterval(this.timer)
            }

            handleStudy = () => {
                ReactDOM.unmountComponentAtNode(document.getElementById('root'))
            }

            render() {
                const {opacity} = this.state

                return (
                    <div>
                        <h2 style={{opacity}}>React学不会怎么办？</h2>
                        <button onClick={this.handleStudy}>拼命学</button>
                    </div>
                )
            }
        }

        ReactDOM.render(<Life />, document.getElementById('root'))
    </script>
</body>
</html>